<form id="edit-form" class="form-horizontal well" action="{{ @BASE }}/issues/save" method="post">
    <csrf-token />
    <check if="{{ !empty(@issue) }}">
        <a href="{{ @BASE }}/issues/{{ @issue.id }}" class="close"><span aria-hidden="true">&times;</span><span class="sr-only">{{ @dict.close }}</span></a>
        <input type="hidden" name="id" value="{{ @issue.id }}">
        <input type="hidden" name="hash_state" value="{{ json_encode(@issue->hashState()) | esc }}">
    </check>
    <input type="hidden" name="type_id" value="{{ @type.id }}">
    <check if="{{ empty(@issue) }}">
        <true>
            <h3>{{ @dict.new_n, isset(@dict[@type.name]) ? @dict[@type.name] : str_replace('_', ' ', @type.name) | format }}</h3>
            <check if="{{ !empty(@parent) }}">
                <h6>{{ @dict.under_n, @parent.name | format }}</h6>
            </check>
        </true>
        <false>
            <h3>{{ @dict.edit_n, isset(@dict[@type.name]) ? @dict[@type.name] : str_replace('_', ' ', @type.name) | format }}</h3>
        </false>
    </check>
    <div class="form-group">
        <label class="col-md-2 control-label">{{ @dict.cols.title }}</label>
        <div class="col-md-7">
            <input type="text" class="form-control" name="name" value="{{ empty(@issue) ? '' : @issue.name | esc }}" autocomplete="off" required>
        </div>
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.size_estimate }}</label>
        <div class="col-md-1">
            <input type="text" class="form-control" name="size_estimate" value="{{ @@issue.size_estimate }}" autocomplete="off" maxlength="20">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.description }}</label>
        <div class="col-md-10">
            <textarea class="form-control input-sm" name="description" rows="7">{{ empty(@issue) ? @type.default_description : @issue.description | esc }}</textarea>
        </div>
    </div>
    <check if="{{ !empty(@issue) }}">
        <div class="form-group">
            <label class="col-md-2 control-label label-sm">{{ @dict.cols.type }}</label>
            <div class="col-md-10">
                <select class="form-control input-sm" name="type_id">
                    <repeat group="{{ @issue_types }}" value="{{ @item }}">
                        <option value="{{ @item.id }}" {{ @issue.type_id == @item.id ? "selected" : '' }}>{{ isset(@dict[@item.name]) ? @dict[@item.name] : str_replace('_', ' ', @item.name) | esc }}</option>
                    </repeat>
                </select>
            </div>
        </div>
    </check>
    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.priority }}</label>
        <div class="col-md-10">
            <select class="form-control input-sm" name="priority">
                <repeat group="{{ @priorities }}" value="{{ @item }}">
                    <check if="{{ empty(@issue) || !isset(@issue.priority) }}">
                        <true>
                            <option value="{{ @item.value }}" {{ @item.value == @issue_priority.default ? "selected" : '' }}>{{ isset(@dict[@item.name]) ? @dict[@item.name] : str_replace('_', ' ', @item.name) | esc }}</option>
                        </true>
                        <false>
                            <option value="{{ @item.value }}" {{ @issue.priority == @item.value ? "selected" : '' }}>{{ isset(@dict[@item.name]) ? @dict[@item.name] : str_replace('_', ' ', @item.name) | esc }}</option>
                        </false>
                    </check>
                </repeat>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.status }}</label>
        <div class="col-md-10">
            <select class="form-control input-sm" name="status">
                <repeat group="{{ @statuses }}" value="{{ @item }}">
                    <option value="{{ @item.id }}" {{ !empty(@issue) && @issue.status == @item.id ? "selected" : "" }}>{{ isset(@dict[@item.name]) ? @dict[@item.name] : str_replace('_', ' ', @item.name) | esc }}</option>
                </repeat>
            </select>
        </div>
    </div>
    <check if="{{ @user.role=='admin' }}">
        <div class="form-group">
            <label class="col-md-2 control-label label-sm">{{ @dict.cols.author }}</label>
            <div class="col-md-10">
                <select class="form-control input-sm" name="author_id">
                    <check if="{{ @@issue.author_id }}">
                        {~ @author = new \Model\User; @author->load(@issue.author_id); ~}
                        <check if="{{ @author.deleted_date }}">
                            <option value="{{ @author.id }}" style="text-decoration: line-through;" selected>{{ @author.name | esc }}</option>
                        </check>
                    </check>
                    <option value="{{ @user.id }}" {{ empty(@issue) ? 'selected' : '' }}>{{ @user.name | esc }}</option>
                    <optgroup label="{{ @dict.groups }}">
                        <repeat group="{{ @groups }}" value="{{ @group }}">
                            <check if="{{ !empty(@issue) && @issue.author_id == @group.id }}">
                                <true><option value="{{ @group.id }}" selected>{{ @group.name | esc }}</option></true>
                                <false><option value="{{ @group.id }}">{{ @group.name | esc }}</option></false>
                            </check>
                        </repeat>
                    </optgroup>
                    <optgroup label="{{ @dict.users }}">
                        <repeat group="{{ @users }}" value="{{ @item }}">
                            <check if="{{ !empty(@issue) && @issue.author_id == @item.id }}">
                                <true><option value="{{ @item.id }}" selected>{{ @item.name | esc }}</option></true>
                                <false><option value="{{ @item.id }}">{{ @item.name | esc }}</option></false>
                            </check>
                        </repeat>
                    </optgroup>
                </select>
            </div>
        </div>
    </check>
    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.assignee }}</label>
        <div class="col-md-10">
            <select class="form-control input-sm" name="owner_id">
                <option value="0">{{ @dict.not_assigned }}</option>
                <check if="{{ @@issue.owner_id ?? @owner_id }}">
                    {~ @owner = new \Model\User; @owner->load(@issue.owner_id ?? @owner_id); ~}
                    <check if="{{ @owner.deleted_date }}">
                        <option value="{{ @owner.id }}" style="text-decoration: line-through;" selected>{{ @owner.name | esc }}</option>
                    </check>
                </check>
                <option value="{{ @user.id }}" {{ empty(@issue) && empty(@owner_id) ? 'selected' : '' }}>{{ @user.name | esc }}</option>
                <optgroup label="{{ @dict.groups }}">
                    <repeat group="{{ @groups }}" value="{{ @group }}">
                        <check if="{{ (@issue.owner_id ?? @owner_id) == @group.id }}">
                            <true><option value="{{ @group.id }}" selected>{{ @group.name | esc }}</option></true>
                            <false><option value="{{ @group.id }}">{{ @group.name | esc }}</option></false>
                        </check>
                    </repeat>
                </optgroup>
                <optgroup label="{{ @dict.users }}">
                    <repeat group="{{ @users }}" value="{{ @item }}">
                        <check if="{{ (@issue.owner_id ?? @owner_id) == @item.id }}">
                            <true><option value="{{ @item.id }}" selected>{{ @item.name | esc }}</option></true>
                            <false><option value="{{ @item.id }}">{{ @item.name | esc }}</option></false>
                        </check>
                    </repeat>
                </optgroup>
            </select>
        </div>
    </div>

    <check if="{{ !empty(@issue) }}">
        <true>
            <div class="form-group">
                <label class="col-md-2 control-label label-sm">{{ @dict.cols.total_spent_hours }}</label>
                <div class="col-md-6">
                    <input class="form-control input-sm" type="text" name="hours_spent" autocomplete="off" value="{{ @issue.hours_spent }}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label label-sm">{{ @dict.cols.hours_total }}</label>
                <div class="col-md-6">
                    <input class="form-control input-sm" type="text" name="hours_total" autocomplete="off" value="{{ @issue.hours_total }}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label label-sm">{{ @dict.cols.hours_remaining }}</label>
                <div class="col-md-6">
                    <input class="form-control input-sm" type="text" name="hours_remaining" autocomplete="off" value="{{ @issue.hours_remaining }}">
                </div>
            </div>
        </true>
        <false>
            <div class="form-group">
                <label class="col-md-2 control-label label-sm">{{ @dict.cols.hours_total }}</label>
                <div class="col-md-6">
                    <input class="form-control input-sm" type="text" name="hours" autocomplete="off">
                </div>
            </div>
        </false>
    </check>

    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.start_date }}</label>
        <div class="col-md-6">
            <input class="form-control input-sm" id="start_date" type="text" name="start_date" autocomplete="off" value="{{ empty(@issue) ? '' : @issue.start_date }}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.due_date }}</label>
        <div class="col-md-6">
            <input class="form-control input-sm" id="due_date" type="text" name="due_date" autocomplete="off" value="{{ empty(@issue) ? '' : @issue.due_date }}">
            <label class="checkbox-inline">
                <input type="checkbox" name="due_date_sprint" value="1" {~ if(@@issue.due_date_sprint) echo 'checked' ~}>
                {{ @dict.set_sprint_from_due_date }}
            </label>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.repeat_cycle }}</label>
        <div class="col-md-6">
            <select class="form-control input-sm" name="repeat_cycle">
                <option value="">{{ @dict.not_repeating }}</option>
                <option value="daily" {{ !empty(@issue) && @issue.repeat_cycle == 'daily' ? "selected" : "" }}>{{ @dict.daily }}</option>
                <option value="weekly" {{ !empty(@issue) && @issue.repeat_cycle == 'weekly' ? "selected" : "" }}>{{ @dict.weekly }}</option>
                <option value="monthly" {{ !empty(@issue) && @issue.repeat_cycle == 'monthly' ? "selected" : "" }}>{{ @dict.monthly }}</option>
                <option value="quarterly" {{ !empty(@issue) && @issue.repeat_cycle == 'quarterly' ? "selected" : "" }}>{{ @dict.quarterly }}</option>
                <option value="semi_annually" {{ !empty(@issue) && @issue.repeat_cycle == 'semi_annually' ? "selected" : "" }}>{{ @dict.semi_annually }}</option>
                <option value="annually" {{ !empty(@issue) && @issue.repeat_cycle == 'annually' ? "selected" : "" }}>{{ @dict.annually }}</option>
                <option value="sprint" {{ !empty(@issue) && @issue.repeat_cycle == 'sprint' ? "selected" : "" }}>{{ @dict.sprint }}</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.parent_id }}</label>
        <div class="col-md-6">
            <input class="form-control input-sm" type="text" name="parent_id" autocomplete="off" value="{{ @@issue.parent_id ?: @@parent.id }}">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label label-sm">{{ @dict.cols.sprint }}</label>
        <div class="col-md-6">
            <select class="form-control input-sm" name="sprint_id">
                <option value="">{{ @dict.no_sprint }}</option>
                <repeat group="{{ @sprints }}" value="{{ @item }}">
                    <check if="{{ !empty(@issue) && @issue.sprint_id == @item.id || empty(@issue) && !empty(@parent) && @parent.sprint_id == @item.id }}">
                        <true>
                            <option value="{{ @item.id }}" selected>{{ @item.name | esc }} &ndash; {{ date('n/j', strtotime(@item.start_date)) }}-{{ date('n/j', strtotime(@item.end_date)) }}</option>
                        </true>
                        <false>
                            <option value="{{ @item.id }}">{{ @item.name | esc }} &ndash; {{ date('n/j', strtotime(@item.start_date)) }}-{{ date('n/j', strtotime(@item.end_date)) }}</option>
                        </false>
                    </check>
                </repeat>
                <check if="{{ !empty(@old_sprints) }}">
                    <optgroup label="{{ @dict.previous_sprints }}">
                        <repeat group="{{ @old_sprints }}" value="{{ @item }}">
                            <check if="{{ !empty(@issue) && @issue.sprint_id == @item.id || empty(@issue) && !empty(@parent) && @parent.sprint_id == @item.id }}">
                                <true>
                                    <option value="{{ @item.id }}" selected>{{ @item.name | esc }} &ndash; {{ date('n/j', strtotime(@item.start_date)) }}-{{ date('n/j', strtotime(@item.end_date)) }}</option>
                                </true>
                                <false>
                                    <option value="{{ @item.id }}">{{ @item.name | esc }} &ndash; {{ date('n/j', strtotime(@item.start_date)) }}-{{ date('n/j', strtotime(@item.end_date)) }}</option>
                                </false>
                            </check>
                        </repeat>
                    </optgroup>
                </check>
            </select>
        </div>
    </div>

    <check if="{{ !empty(@issue) }}">
        <true>
            {~ \Helper\Plugin::instance()->callHook('render.issue_edit.after_fields', @issue) ~}
        </true>
        <false>
            {~ \Helper\Plugin::instance()->callHook('render.issue_create.after_fields') ~}
        </false>
    </check>

    <hr>
    <check if="{{ !empty(@issue) }}">
        <div class="form-group">
            <label class="col-md-2 control-label label-sm">{{ @dict.comment }}</label>
            <div class="col-md-10">
                <textarea class="form-control input-sm" name="comment" rows="3">{{ !empty(@comment) && is_string(@comment) ? @comment : "" | esc }}</textarea>
            </div>
        </div>

        <check if="{{ !empty(@issue) }}">
            {~ \Helper\Plugin::instance()->callHook('render.issue_edit.after_comments', @issue) ~}
        </check>
    </check>

    <div class="row">
        <div class="col-md-offset-2 col-md-10 clearfix">
            <div class="pull-right" style="margin-left: 15px;">
                <check if="{{ !empty(@issue) }}">
                    <a href="{{ @BASE }}/issues/{{ @issue.id }}" class="btn btn-default btn-sm btn-cancel">{{ @dict.cancel }}</a>
                    <button type="reset" class="btn btn-default btn-sm">{{ @dict.reset }}</button>
                </check>
                <check if="{{ empty(@issue) }}">
                    <true>
                        <button type="submit" class="btn btn-primary btn-sm">{{ @dict.create_n, isset(@dict[@type.name]) ? @dict[@type.name] : str_replace('_', ' ', @type.name) | format }}</button>
                    </true>
                    <false>
                        <button type="submit" class="btn btn-primary btn-sm">{{ @dict.save_n, isset(@dict[@type.name]) ? @dict[@type.name] : str_replace('_', ' ', @type.name) | format }}</button>
                    </false>
                </check>
            </div>
            <div class="pull-right">
                <div class="checkbox checkbox-container-inline">
                    <label>
                        <input type="checkbox" name="notify" value="1" checked>
                        {{ @dict.send_notifications }}
                    </label>
                </div>
            </div>
        </div>
    </div>
</form>
